var app = getApp();
Page({
  data: {
  	orderList:[],
  	type:'',//订单状态0全部 1待付款 2待收货 3待自提 4已完成 -1已取消
  	page: 1,
    pages: 100,
    limit:5
  },
  onLoad(options) {
  	var that=this;
  	if(options){
	  	if(options.type){
	  		var type=options.type;
		  	that.setData({
			      type:type
			  })
	  	}
  	}
  	// 页面初次加载，请求第一页数据
    this.fetchArticleList(1, true,function(e){})
  },
  type_select:function(e){
  	var that=this;
  	var i=e.currentTarget.dataset.index;
  	that.setData({
	      type:i,
	      page:1
	  })
  	this.fetchArticleList(1, true,function(e){})
  },
  ReachBottom() {
  	console.log("触底")
    // 下拉触底，先判断是否有请求正在进行中
    // 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
    if (!this.loading && this.data.page < this.data.pages) {
    	this.data.page+=1;
      this.fetchArticleList(this.data.page, false,function(e){})
    }
  },
  onPullDownRefresh() {
    // 上拉刷新
    console.log(this.loading)
    if (!this.loading) {
      this.fetchArticleList(1, true,function(){
      	wx.stopPullDownRefresh();
      })
    }
  },
  fetchArticleList(pageNo, override,call) {
  	var $th=this;
    $th.loading = true
    // 向后端请求指定页码的数据
    console.log(pageNo, override,call)
    if(override){//重置list
    	$th.data.goodsList=[];
    }
    if(call){
    	var t=$th.data.type;
	  	if(t==0) t='';
    	var post={
    		status:t,
    		page:pageNo,
    		limit:$th.data.limit
    	}
    	
    	app.globalAjax(
	  		'/api/order/list',
	  		"get",
	  		post,
	  		function(e){
	  			if(e.code==0)
	      	{
	      		var list=[];
	      		if(pageNo!=1){
	      			list=$th.data.orderList
	      		}
	      		var c=list.concat(e.orderList);
	      		console.log(c)
	      		$th.setData({
					      orderList:c
					  })
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				      });
	      	}
	  	})
    	
    	setTimeout(function(){
    		$th.loading=false;
    		call();
    	},1000)
    }
  },
  Qpay:function(e){//订单付款
  	var i=e.target.dataset.index,
  			that=this,
  			$th=that.data.orderList[i]
		  	app.globalAjax(
			  		'/api/unifiedOrder',
			  		"post",
			  		{
			  			orderNumber:$th.orderNumber,
			  			totalAmount:0.1
			  		},
			  		function(e){
			  			if(e.code==0)
			      	{
			      		app.pays(e.data,function(ee){
			      			that.onLoad()
			      		},function(){
			      			
			      		})
			      	}else{
			      		wx.showToast({
						        title: e.msg,
						        icon: "none"
						      });
			      	}
			  },{token:wx.getStorageSync("token")})
  },
  gotoclass:function(){//去购物
  	var pages = getCurrentPages(); // 获取页面栈
		var homePage = pages[0]; // 首页页面
		homePage.onLoad({cur:'classify'})
		console.log(pages.length-1)
		wx.navigateBack({delta: pages.length-1})
  }
})